package com.tiendung.otalunch;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.RatingBar;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {        
	@Override     
	public boolean areAllItemsEnabled()      
	{          
		return true;      
	}        
	
	private Context context;        
	private ArrayList<String> groups;        
	private ArrayList<ArrayList<Food>> children;     
	
	public ExpandableListAdapter(Context context, ArrayList<String> groups, ArrayList<ArrayList<Food>> children) 
	{          
		this.context = context;          
		this.groups = groups;          
		this.children = children;      
	} 
	
	 public HashMap<String, Integer> addItem(Food food) 
	 {
		 HashMap<String, Integer> food_index = new HashMap<String, Integer>();
		 
		 if (!groups.contains(food.getRestaurant())) 
			 groups.add(food.getRestaurant());     
		 
		 int index = groups.indexOf(food.getRestaurant());
		 if (children.size() < index + 1) 
		 {              
			 children.add(new ArrayList<Food>());          
		 }          
		 
		 children.get(index).add(food);
		 
		 food_index.put("Group", index);
		 food_index.put("Child", children.get(index).size() - 1);
		 		 
		 return food_index;
	 }        
	 
	 @Override      
	 public Object getChild(int groupPosition, int childPosition) 
	 {          
		 return children.get(groupPosition).get(childPosition);      
	 }        
	 
	 @Override      
	 public long getChildId(int groupPosition, int childPosition) 
	 {          
		 return childPosition;      
	 }            
	 
	 // Return a child view. You can load your custom layout here.      
	 @Override      
	 public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) 
	 {          
		 Food food = (Food) getChild(groupPosition, childPosition);          
		 
		 if (convertView == null) 
		 {              
			 LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);              
			 convertView = infalInflater.inflate(R.layout.child_layout, null);          
		 }          
		 
		 TextView tv1 = (TextView) convertView.findViewById(R.id.tvChild);          
		 tv1.setText("   " + food.getName());   
		 
		 TextView tv2 = (TextView) convertView.findViewById(R.id.tvChildChild);          
		 tv2.setText("   (" + food.getRestaurant() + ")");  
		 
		 RatingBar rb = (RatingBar) convertView.findViewById(R.id.ratingbar);
		 rb.setRating(food.getRate());
		 
		 return convertView;      
	 }        
	 
	 @Override      
	 public int getChildrenCount(int groupPosition) 
	 {          
		 return children.get(groupPosition).size();      
	 }        
	 
	 @Override      
	 public Object getGroup(int groupPosition) 
	 {          
		 return groups.get(groupPosition);     
	 }        
	 
	 @Override      
	 public int getGroupCount() 
	 {          
		 return groups.size();      
	 }        
	 
	 @Override      
	 public long getGroupId(int groupPosition) 
	 {          
		 return groupPosition;      
	 }        
	 
	 // Return a group view. You can load your custom layout here.      
	 @Override     
	 public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) 
	 {          
		 String group = (String) getGroup(groupPosition);          
		 
		 if (convertView == null) 
		 {              
			 LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);              
			 convertView = infalInflater.inflate(R.layout.group_layout, null);          
		 }          
		 
		 TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);          
		 tv.setText(group);          
		 return convertView;   
	 }        
	 
	 @Override      
	 public boolean hasStableIds() 
	 {          
		 return true;      
	 }        
	 
	 @Override      
	 public boolean isChildSelectable(int arg0, int arg1) 
	 {          
		 return true;      
	 } 
	 
}